﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaaSKit.Taskr.Service
{
    public static class TaskDTOExtensions
    {
        public static Task MapTo(this TaskDTO dto)
        {
            return new Task()
            {
                Status = dto.Status,
                Summary = dto.Summary,
                Description = dto.Description,
                DueDate = dto.DueDate,
                AssigneeId = dto.AssigneeId,
                CompletedDate = dto.CompletedDate
            };
        }

        public static void MapInto(this TaskDTO dto, Task instance)
        {
            instance.AssigneeId = dto.AssigneeId;
            instance.DueDate = dto.DueDate;
            instance.CompletedDate = dto.CompletedDate;
            instance.Status = dto.Status;
            instance.Summary = dto.Summary;
            instance.Description = dto.Description;
        }

        public static void MapFrom(this TaskDTO dto, Task instance)
        {
            dto.MapFrom(instance, false);
        }

        public static void MapFrom(this TaskDTO dto, Task instance, bool fullyHydrate)
        {
            if(instance == null) 
            {
                return;
            }

            dto.Id = instance.Id;
            dto.AssigneeId = instance.AssigneeId;
            dto.CreatedDate = instance.CreatedDate;
            dto.DueDate = instance.DueDate;
            dto.CompletedDate = instance.CompletedDate;
            dto.Status = instance.Status;
            dto.Summary = instance.Summary;
            dto.Description = instance.Description;

            if(fullyHydrate)
            {
                dto.ActivityHistory = new List<ActivityDTO>();

                foreach (Activity activity in instance.ActivityHistory)
                {
                    ActivityDTO activityDTO = new ActivityDTO();
                    activityDTO.MapFrom(activity);
                    dto.ActivityHistory.Add(activityDTO);
                }
            }
        }
    }
}
